import axios from 'axios'
import router from '../router'
import store from '../store'

axios.interceptors.response.use((res) => {
  const code = String(res.data.code)
  if (code === '403') router.replace({ path: '/login' })
  return res
}, (err) => {
  // const status = err.response.status
  // console.log(err)
  // if (status === '404') router.replace({path: 'error'})
  return Promise.reject(err.response)
})

axios.interceptors.request.use((req) => {
  if (req.url !== '/api/authserver/user/login' && req.url.indexOf('http://') === -1) {
    req.headers.Authorization = store.state.token || localStorage.getItem('token');
    req.url = '/api/uplus' + req.url;
    //req.url = 'http://47.105.161.41/api/uplus-test' + req.url;
  }
  // req.headers.Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNTQzNTk3Mzg1LCJleHAiOjE1NDQyMDIxODV9.GslhjEdEqbP8nBnqtm6wkhYGefO7hPCLfATcj28rkJp9xwuBQX2F2-jwQyX8YZ7QsPYNxfgIYa3nncFIqazX8w'
  return req
}, (err) => {
  return Promise.reject(err)
})
export default axios;
